package han.bifang.web.servlet;

import han.bifang.business.config.ConfigManager;
import han.bifang.business.sec.access.AuthenticatorFactory;
import han.bifang.business.sec.rbac.RbacProviderFactory;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaConfig;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Servlet implementation class for Servlet: InitServlet
 *
 */
 public class InitServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public InitServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	  	  	  
	
	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() throws ServletException {
		
		super.init();
		try {
			List<SoaConfig> l = ConfigManager.getConfigByType(EntityConstants.CONFIG_TYPE_SECURITY_PROVIDER);
//			l.get(EntityConstants.CONFIG_RBAC_PROVIDER)
			HashMap<String, SoaConfig> list = new HashMap<String, SoaConfig>();
			for (SoaConfig cn : l) {
				list.put(cn.getKey(), cn);
			}
			RbacProviderFactory.setProviderClass(list.get(EntityConstants.CONFIG_RBAC_PROVIDER).getValue());
			AuthenticatorFactory.setAuthenticatorClass(list.get(EntityConstants.CONFIG_ACCESS_PROVIDER).getValue());
		} catch (Exception e) {
			Log logger = LogFactory.getLog(InitServlet.class);
			logger.error("InitServlet.init()", e);
			throw new ServletException(e);
		}
	}   
}